/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cetc.iframe;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import com.cetc.Item;
import com.cetc.dao.Dao;
import com.cetc.dao.model.TbKucun;

public class JiaGeTiaoZheng extends javax.swing.JInternalFrame {

    /**
     * Creates new form JiaGeTiaoZheng
     */
    public JiaGeTiaoZheng() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jFrame1 = new javax.swing.JFrame();
        jMenu3 = new javax.swing.JMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jLabel2 = new javax.swing.JLabel();
        spmc = new javax.swing.JComboBox<>();
        okButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        dj = new javax.swing.JTextField();
        kcje = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        kcsl = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        gg = new javax.swing.JLabel();
        cd = new javax.swing.JLabel();
        jc = new javax.swing.JLabel();
        bz = new javax.swing.JLabel();
        dw = new javax.swing.JLabel();

        jMenu1.setText("jMenu1");

        jMenu2.setText("jMenu2");

        jMenuItem1.setText("jMenuItem1");

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jMenu3.setText("jMenu3");

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("价格调整");
        setPreferredSize(new java.awt.Dimension(600, 280));

        jLabel2.setText("商品名称：");
        
        spmc.addItemListener(new ItemListener() {// 为“商品名称”下拉列表添加选项事件的监听
			public void itemStateChanged(final ItemEvent e) {
				Object selectedItem = spmc.getSelectedItem();// 获得“商品名称”下拉列表被选中的选项
				if (selectedItem == null)// “商品名称”下拉列表被选中的选项不存在
					return;// 退出应用程序
				Item item = (Item) selectedItem;// 获得数据表公共类
				kcInfo = Dao.getKucun(item);// 获取库存信息
				if (kcInfo.getId() == null)// 库存信息中的商品编号不存在
					return;// 退出应用程序
				int djje, sl;// 声明“单价”、“库存数量”
				djje = kcInfo.getDj().intValue();// 为“单价”赋值
				sl = kcInfo.getKcsl().intValue();// 为“库存数量”赋值
				cd.setText(kcInfo.getCd());// 设置“产地”标签里的文本内容
				jc.setText(kcInfo.getJc());// 设置“简称”标签里的文本内容
				bz.setText(kcInfo.getBz());// 设置“包装”标签里的文本内容
				dw.setText(kcInfo.getDw());// 设置“单位”标签里的文本内容
				dj.setText(kcInfo.getDj() + "");// 设置“单价”文本框里的文本内容
				kcsl.setText(kcInfo.getKcsl() + "");// 设置“库存数量”文本框里的文本内容
				kcje.setText(djje * sl + "");// 设置“库存金额”文本框里的文本内容
				gg.setText(kcInfo.getGg());// 设置“规格”标签里的文本内容
			}
		});

        addInternalFrameListener(new InternalFrameAdapter() {// 添加内部窗体的监听事件
			public void internalFrameActivated(final InternalFrameEvent e) {
				DefaultComboBoxModel mingChengModel = (DefaultComboBoxModel) spmc.getModel();// 下拉列表的默认模型
				mingChengModel.removeAllElements();// 清空列表
				List list = Dao.getKucunInfos();// 获得库存信息的集合
				Iterator iterator = list.iterator();// 获得与集合list对应的迭代器
				while (iterator.hasNext()) {// 被迭代的集合元素是否被遍历完
					List element = (List) iterator.next();// 获得集合中的下一个元素
					Item item = new Item();// 获得数据表公共类
					item.setId((String) element.get(0));// 设置编号属性
					item.setName((String) element.get(1));// 设置名称信息
					mingChengModel.addElement(item);// 把数据表公共类的对象添加到下拉列表中
				}
			}
		});

        okButton.setText("确定");
        
        okButton.addActionListener(new ActionListener() {// 为“确定”按钮添加动作事件的监听
			public void actionPerformed(final ActionEvent e) {
				kcInfo.setDj(Double.valueOf(dj.getText()));// 重置库存信息类中的单价
				kcInfo.setKcsl(Integer.valueOf(kcsl.getText()));// 重置库存信息类中的库存数量
				int rs = Dao.updateKucunDj(kcInfo);// 获得修改库存的记录条数
				if (rs > 0)// 修改库存的记录条数大于0
					JOptionPane.showMessageDialog(getContentPane(), "价格调整完毕。", kcInfo.getSpname() + "价格调整",
							JOptionPane.QUESTION_MESSAGE);// 弹出“价格调整完毕”提示框
			}
		});

        closeButton.setText("关闭");
        
        closeButton.addActionListener(new ActionListener() {// 为“关闭”按钮添加动作事件的监听
			public void actionPerformed(final ActionEvent e) {
				JiaGeTiaoZheng.this.doDefaultCloseAction();// 关闭价格调整窗体
			}
		});

        jLabel12.setText("产    地：");

        jLabel13.setText("包    装：");

        jLabel14.setText("单    价：");
        dj.addKeyListener(new KeyAdapter() {// 为“单价”文本框添加键盘事件的监听
			public void keyReleased(final KeyEvent e) {
				updateJinE();// 更改库存金额的方法
			}
		});

        jLabel15.setText("库存金额：");
        kcje.setEditable(false);

        jLabel16.setText("库存数量：");
        kcsl.setEditable(false);

        jLabel17.setText("规    格：");

        jLabel18.setText("简    称：");

        jLabel19.setText("单    位：");

        gg.setForeground(Color.BLUE);// 设置字体颜色为蓝色

        cd.setForeground(Color.BLUE);// 设置字体颜色为蓝色

        jc.setForeground(Color.BLUE);// 设置字体颜色为蓝色

        bz.setForeground(Color.BLUE);// 设置字体颜色为蓝色

        dw.setForeground(Color.BLUE);// 设置字体颜色为蓝色

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel13)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bz)
                                .addGap(180, 180, 180)
                                .addComponent(jLabel19))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cd)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel18))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(spmc, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(30, 30, 30)
                                .addComponent(jLabel17))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel14)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(dj, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel16)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(kcsl, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(gg)
                                    .addComponent(jc)
                                    .addComponent(dw))
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel15)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(kcje, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(180, 180, 180)
                                .addComponent(okButton)
                                .addGap(80, 80, 80)
                                .addComponent(closeButton)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spmc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel17)
                    .addComponent(gg))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel18)
                        .addComponent(jc))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel12)
                        .addComponent(cd)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jLabel19)
                    .addComponent(bz)
                    .addComponent(dw))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel16)
                        .addComponent(kcsl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel14)
                        .addComponent(dj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel15)
                    .addComponent(kcje, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(closeButton))
                .addGap(42, 42, 42))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void updateJinE() {// 更改库存金额的方法
		try {
			Double djje = Double.valueOf(dj.getText());// 将“单价”文本框中的内容转换为Double型
			Integer sl = Integer.valueOf(kcsl.getText());// 将“库存数量”文本框中的内容转换为Integer型
			kcje.setText((djje * sl) + "");// 更改“库存金额”文本框中的内容
		} catch (Exception e) {
			JOptionPane.showMessageDialog(JiaGeTiaoZheng.this, "单价格式错误！");
			return;
		}
	}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JiaGeTiaoZheng.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JiaGeTiaoZheng.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JiaGeTiaoZheng.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JiaGeTiaoZheng.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JiaGeTiaoZheng().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel bz;
    private javax.swing.JLabel cd;
    private javax.swing.JTextField dj;
    private javax.swing.JLabel dw;
    private javax.swing.JLabel gg;
    private javax.swing.JButton okButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JLabel jc;
    private javax.swing.JTextField kcje;
    private javax.swing.JTextField kcsl;
    private javax.swing.JComboBox spmc;
    private TbKucun kcInfo;// 库存信息
    // End of variables declaration//GEN-END:variables
}
